﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 边界框区域
/// </summary>
[System.Serializable] 
public class Border
{
	public int left;		//左边界
	public int right;		//右边界
	public int low;			//下边界
	public int hight;		//上边界
	public int width;		//整个地图的宽
	
	public StandardResolution sr;	//标准分辨率
}

/// <summary>
/// 标准分辨率
/// </summary>
[System.Serializable]
public class StandardResolution
{
	public int width = 720;
	public int height = 1280;
}

public class ViewRect : MonoBehaviour{
	
	public Border border;			//边界框区域
	
	public Rect viewRect;
	public Vector3 heroScaleV = Vector3.one;	//hero的大小 scale
	public Transform map;
	
	Rect rect;
	Rect borderRect;
	
	static ViewRect instance;
	public static ViewRect GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;
		rect = viewRect;
	}
	
	public Rect ViewRectView
	{
		get{return viewRect;}
	}
	
	/// <summary>
	/// 边界区域框视图
	/// </summary>
	public Border BorderView
	{
		get{return border;}
	}
	
	/// <summary>
	/// hero 大小的缩放
	/// </summary>
	public Vector3 HeroScaleV
	{
		get{return heroScaleV;}
	}
	
	/// <summary>
	/// 是否在矩形视野范围内, 不包含计算地图信息
	/// </summary>
	public bool IsInViewRect(Vector2 pos)
	{
		return ViewRectView.Contains(pos);
	}
	
	/// <summary>
	/// 是否在视野范围内, 包含计算地图位置
	/// </summary>
	public bool IsInViewRectContainMapPos(Vector2 pos)
	{
		rect.x = viewRect.x + map.transform.localPosition.x;
		rect.y = viewRect.y - map.transform.localPosition.y;
		
		return rect.Contains(pos);
	}
	
	/// <summary>
	/// 获得边界矩形框
	/// </summary>
	public Rect BorderRect()
	{
//		if(borderRect != null)
//			return borderRect;
		
//		borderRect = new Rect(-border.width * 0.5f + border.left, 
//			border.low, border.width - border.left - border.right,
//			Screen.height - border.low - border.hight);
		

//		float scale = (float)border.sr.width / Screen.width;
		float offSetX = (border.sr.width - Screen.width) * 0.5f;
		
//		int left = (int)(border.left * scale);
//		int right = (int)(border.right * scale);
		int left = (int)(border.left + offSetX);
		int right = (int)(border.right + offSetX);
		
		borderRect = new Rect(-border.width * 0.5f + left, 
			Screen.height - border.hight, border.width - left - right,
			Screen.height - border.low - border.hight);
		
		Debug.Log(borderRect + "   ~~~~~  " + "     " + Screen.width);
		return borderRect;
	}
}
